
//
//  PingJiaViewController.m
//  yonghu
//
//  Created by 浪尖渝力 on 2018/1/22.
//  Copyright © 2018年 com.DJS. All rights reserved.
//

#import "PingJiaViewController.h"

@interface PingJiaViewController ()<UITextViewDelegate>
@property(nonatomic,strong)NSMutableArray<UIImageView *> *btnArr;
@property(nonatomic,strong)NSNumber *scorl;

@property(nonatomic,strong)UITextView *textView;
@property(nonatomic,strong)UILabel *numLabel;
@end

@implementation PingJiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"评价";
    _scorl=@0;
    UILabel *label=[UILabel new];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(NAV_HEIGHT+25);
    }];
    label.text=@"评分:";
    label.font=[UIFont systemFontOfSize:18];
    label.textColor=ColorWithRGB(149, 149, 149);
    
    _btnArr=[NSMutableArray arrayWithCapacity:0];
    
    for (int i=0; i<5; i++) {
        UIImageView *xing=[UIImageView new];
        [self.view addSubview:xing];
        [xing mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(label);
            make.left.mas_equalTo(label.mas_right).mas_equalTo(35*i+15);
            make.width.height.mas_equalTo(26);
        }];
        xing.tag=i+1;
        xing.image=[UIImage imageNamed:@"评分1"];
        
        xing.userInteractionEnabled=YES;
        UITapGestureRecognizer *tuiChuView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Xing:)];
        [xing addGestureRecognizer:tuiChuView];
        
        [_btnArr addObject:xing];
    }
    
    
    _textView=[UITextView new];
    [self.view addSubview:_textView];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(240);
        make.top.mas_equalTo(label.mas_bottom).mas_equalTo(40);
        
    }];
    _textView.backgroundColor=_LightGrey;
    _textView.returnKeyType=UIReturnKeyDone;
    _textView.delegate=self;
    _textView.text=@"请输入内容...";
    _textView.textColor=ColorWithRGB(149, 149, 149);
    _textView.font=[UIFont systemFontOfSize:18];
    
    _numLabel=[UILabel new];
    [self.view addSubview:_numLabel];
    [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(_textView.mas_bottom).mas_equalTo(-5);
    }];
    _numLabel.textColor=ColorWithRGB(149, 149, 149);
    _numLabel.font=[UIFont systemFontOfSize:14];
    _numLabel.text=@"0/200";
    
    UIButton *btn1=[UIButton new];
    [self.view addSubview:btn1];
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(_mainW-30);
        make.height.mas_equalTo(50);
        make.bottom.mas_equalTo(-20);
    }];
    btn1.layer.cornerRadius=10;
    [btn1 setTitle:@"立即评价" forState:0];
    [btn1 setTitleColor:[UIColor whiteColor] forState:0];
    btn1.backgroundColor=_backgroundColor;
    [btn1 addTarget:self action:@selector(pijiama:) forControlEvents:UIControlEventTouchUpInside];
}
-(IBAction)pijiama:(id)sender{
    NSString *evaluate_content=_textView.text;
    if ([ZQTools charIsNil:evaluate_content]||[evaluate_content isEqualToString:@"请输入内容..."]) {
        [ZQTools svpInfo:@"请输入你的评价内容"];
        return;
    }
    if (_scorl.intValue==0) {
        [ZQTools svpInfo:@"你还没有评分"];
        return;
    }
    
    
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    NSString *token=[userDic objectForKey:@"token"];
    NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
    NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:user_id,@"user_id",token,@"token",@"0",@"user_role",[NSString stringWithFormat:@"%@",_evaluate_services_id],@"evaluate_services_id",[NSString stringWithFormat:@"%@",_evaluate_services_type],@"evaluate_services_type",[NSString stringWithFormat:@"%@",_evaluate_waiter_id],@"evaluate_waiter_id",evaluate_content,@"evaluate_content",[NSString stringWithFormat:@"%@",_scorl],@"evaluate_score", nil];
    [ZQTools AFNPostDataUrl:@"user/evaluate/add_evaluate" Dict:dic andTableView:nil andViewController:self
            andSuccessBlock:^(id responseObject) {
                
                NSNotification * notice = [NSNotification notificationWithName:@"pingjia2" object:nil userInfo:nil];
                [[NSNotificationCenter defaultCenter]postNotification:notice];
                [ZQTools svpInfo:@"评价成功"];
                [self.navigationController popViewControllerAnimated:YES];
            } anderrorBlock:nil];
    
}

-(void)Xing:(UITapGestureRecognizer *)xing{
    int tag=(int)xing.view.tag;
    _scorl=[NSNumber numberWithInt:tag];
    for (int i=0; i<_btnArr.count; i++) {
        UIImageView *btn=_btnArr[i];
        if (i<tag) {
            btn.image=[UIImage imageNamed:@"评分2"];
            
        }
        else{
           btn.image=[UIImage imageNamed:@"评分1"];
        }
    }
    
}


-(void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@"请输入内容..."]) {
        textView.text=@"";
    }
    textView.textColor=ColorWithRGB(49, 49, 40);
    
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@""]) {
        textView.text=@"请输入内容...";
        textView.textColor=ColorWithRGB(220 , 220, 220);
    }
    else{
        textView.textColor=ColorWithRGB(49 , 59, 49);
    }
    
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    _numLabel.text=[NSString stringWithFormat:@"%ld/200",textView.text.length - range.length + text.length];
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        
        [_textView resignFirstResponder];        //在这里做你响应return键的代码
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    if ((textView.text.length - range.length + text.length) > 200)
    {
        
        
        NSString *substring = [text substringToIndex:200 - (textView.text.length - range.length)];
        NSMutableString *lastString = [textView.text mutableCopy];
        [lastString replaceCharactersInRange:range withString:substring];
        textView.text = [lastString copy];
        return NO;
    }
    else
    {
        return YES;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
